package com.factory.test.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.base.BaseTestActivity;
import com.factory.test.R;
import com.factory.test.databinding.ActivityUsbBinding;

import java.util.List;
import java.util.Locale;

/**
 * U盘测试
 */
public class UsbActivity extends BaseTestActivity {

    private ActivityUsbBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_usb);

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_MEDIA_EJECT);
        filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        filter.addDataScheme("file");
        registerReceiver(usbDiskReceiver, filter);

        updateMessage();

        binding.open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoFileManager();
            }
        });
    }


    private void gotoFileManager(){
        try {
            Intent rockExplorer = new Intent();
            rockExplorer.setClassName("com.android.rk", "com.android.rk.RockExplorer");
            startActivity(rockExplorer);
        } catch (Exception e) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivity(intent);
        }
    }


    /**
     * 下一版本, 增加USB, 显示U盘数量
     */
    private void updateMessage(){
        List<String> paths = sdkService.getUsbRootPaths();
        if(path1 == null){
            binding.message.setText("U盘未接入");
            binding.message.setTextColor(Color.RED);
        }else{
            binding.message.setText(String.format(Locale.CHINA, "U盘已接入 (%d)", paths.size()));
            binding.message.setTextColor(Color.GREEN);
        }
    }

    private Runnable updateRunnable = new Runnable() {
        @Override
        public void run() {
            updateMessage();
        }
    };

    String path1=null;
    private BroadcastReceiver usbDiskReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("TAG", "onReceive: "+intent.getAction());
            if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)){
            path1 = intent.getData().getPath();
            Log.d("TAG", "U盘: "+path1);
            handler.removeCallbacks(updateRunnable);
            handler.postDelayed(updateRunnable, 500);
            } else if (intent.getAction().equals(Intent.ACTION_MEDIA_EJECT)) {
                binding.message.setText("U盘未接入");
                binding.message.setTextColor(Color.RED);
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(usbDiskReceiver);
    }
}
